package com.tencent.qcloud.timchat.model;

import android.content.Context;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.app.LoginInfo;
import com.bxd.baodan.item.ChatQABean;
import com.bxd.baodan.util.GsonUtil;
import com.bxd.baodan.util.UrlUtils;
import com.tencent.TIMCustomElem;
import com.tencent.TIMMessage;
import com.tencent.qcloud.timchat.adapters.ChatAdapter;

import java.io.UnsupportedEncodingException;

/**
 * Created by lizhichao on 18/2/7.
 */

public class AutoQAMessage extends Message {
   public ChatQABean.QABean bean;

   String content;



    public AutoQAMessage(TIMMessage message) {
        this.message = message;
        TIMCustomElem elem = (TIMCustomElem) message.getElement(0);
        parse(elem.getData());
    }

    private void parse(byte[] data) {
        try {
            String str = new String(data, "UTF-8");
            content = str;
            bean = GsonUtil.fromJson(str, ChatQABean.QABean.class);


        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();

            return;
        }

    }

    @Override
    public void showMessage(ChatAdapter.ViewHolder viewHolder, Context context) {


        clearView(viewHolder);

        if (bean != null) {


            viewHolder.message_no_read_count.setVisibility(View.GONE);
            boolean hasText = false;
            if (isSelf()) {
                Glide.with(context).load(LoginInfo.getInstance(context).getUser().getUe_avatar()).error(R.drawable.empty_avatar).into(viewHolder.rightAvatar);
            } else {
                Glide.with(context).load(viewHolder.toShowUrl).error(R.drawable.empty_avatar).into(viewHolder.leftAvatar);
            }

            TextView tv = new TextView(App.getContext());
            tv.setPadding(10, 10, 10, 10);
            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            tv.setTextColor(App.getContext().getResources().getColor(isSelf() ? R.color.white : R.color.black));
            String content = bean.title;
            UrlUtils.handleText(tv, content);
            getBubbleView(viewHolder).addView(tv);
            showStatus(viewHolder);
        } else {
            TextView v = new TextView(context);
            ((TextView)v).setText("消息类型异常");
            getBubbleView(viewHolder).addView(v);
        }

    }

    @Override
    public String getSummary() {
        return null;
    }

    @Override
    public void save() {

    }



}
